package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.ClientLost;
import cn.wolfcode.crm.mapper.ClientLostMapper;
import cn.wolfcode.crm.mapper.StudentOfficialMapper;
import cn.wolfcode.crm.query.ClientLostQueryObject;
import cn.wolfcode.crm.service.IClientLostService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClientLostServiceImpl implements IClientLostService {
    @Autowired
    private ClientLostMapper clientLostMapper;

    @Autowired
    private StudentOfficialMapper studentOfficialMapper;


    @Override
    public void deleteByPrimaryKey(Long id) {
        clientLostMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(ClientLost entity) {
        clientLostMapper.insert(entity);
    }

    @Override
    public ClientLost selectByPrimaryKey(Long id) {

        return clientLostMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ClientLost> selectAll() {
        return clientLostMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(ClientLost entity) {
        clientLostMapper.updateByPrimaryKey(entity);
    }


    //流失客户审核
    @Override
    public void changState(Long id, Long clientId) {
        clientLostMapper.changState(id);
        //客户流失后隐藏正式学员管理中的信息
        studentOfficialMapper.changeClientState(clientId);
    }

    @Override
    public PageResult query(ClientLostQueryObject qo) {
        Integer total = clientLostMapper.queryForCount(qo);
        if (total == 0) {
            return PageResult.PAGERESULT_EMPTY;
        }
        List<?> query = clientLostMapper.query(qo);
        return new PageResult(total, query);
    }

    @Override
    public Workbook exportXls(ClientLostQueryObject qo) {
        //创建xls
        Workbook book = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = book.createSheet();
        //设置第一行的标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("学员名称");
        row.createCell(1).setCellValue("联系电话");
        row.createCell(2).setCellValue("班级名称");
        row.createCell(3).setCellValue("流失阶段");
        row.createCell(4).setCellValue("流失原因");
        row.createCell(5).setCellValue("流失时间");
        row.createCell(6).setCellValue("学习天数");
        row.createCell(7).setCellValue("是否退款");
        row.createCell(8).setCellValue("销售名称");
        row.createCell(9).setCellValue("审核人");
        row.createCell(10).setCellValue("状态");

        List<?> list = clientLostMapper.query(qo);

        for (int i = 0; i < list.size(); i++) {
            try {
                ClientLost cl = (ClientLost) list.get(i);
                //创建行
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(cl.getClientName());
                row.createCell(1).setCellValue(cl.getTel());
                row.createCell(2).setCellValue(cl.getClassname());
                row.createCell(3).setCellValue(cl.getLoststep());
                row.createCell(4).setCellValue(cl.getLostcause());
                row.createCell(5).setCellValue(cl.getLostdate());
                row.createCell(6).setCellValue(cl.getStudyday() == null ? 0 : cl.getStudyday());
                row.createCell(7).setCellValue(cl.getReturnOrNot() == null ? "否" : "是");
                row.createCell(8).setCellValue(cl.getSale());
                row.createCell(9).setCellValue(cl.getChecker());
                row.createCell(10).setCellValue(cl.getState() ? "是" : "否");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return book;
    }
}
